﻿

(function() {
    
    AjaxControlFramework.CreateType('AjaxControlFramework.UpdateRegion');

	AjaxControlFramework.UpdateRegion = function(properties, initialState) {
        AjaxControl.call(this, properties, initialState);
		this.Properties.UpdatedHandlers = [];
		this.Properties.UpdatedHandlers.DoNotSerialize = true;
    };

	window.$ur = AjaxControlFramework.UpdateRegion;
	$ur.prototype = new AjaxControl;
	$ur.prototype.constructor = $ur;


	AjaxControlFramework.UpdateRegion.RegisteredRegions = {};


	$ur.prototype.GetType = function() {
		return "AjaxControlFramework.UpdateRegion, AjaxControlFramework";
	};


	$ur.prototype.FireUpdatedHandlers = function(result) {
		var updatedHandlers = this.Properties.UpdatedHandlers;

		if (updatedHandlers != null && updatedHandlers.length && updatedHandlers.length > 0)
		{
			for (var i = 0; i < updatedHandlers.length; i++)
			{
				updatedHandlers[i](result);
			}
		}
	};
    
    $ur.prototype.Update = function(onSuccess, onFailure) {
		if (onSuccess == null)
		{
			onSuccess = this.FireUpdatedHandlers;
		}
		else
		{
			var oldSuccess = onSuccess;

			onSuccess = function(result) {
				oldSuccess(result);
				this.FireUpdatedHandlers(result);
			};
		}

        AjaxControl.prototype.Update.apply(this, [{ OnFailure: onFailure, OnSuccess: onSuccess }]);
    };

	$ur.prototype.AddUpdatedHandler = function(handler) {
		this.Properties.UpdatedHandlers.push(handler);
	};

	$ur.prototype.Register = function() {
		AjaxControl.prototype.Register.apply(this);
		AjaxControlFramework.UpdateRegion.RegisteredRegions[this.ClientID()] = this;
	};

	$ur.prototype.Unregister = function() {
		AjaxControl.prototype.Unregister.apply(this);
		delete AjaxControlFramework.UpdateRegion.RegisteredRegions[this.ClientID()];
	};
    
    delete $ur.prototype.Load;
    delete $ur.prototype.Invoke;
    delete $ur.prototype.GlobalInvoke;

    
    
    if (typeof window["__doPostBack"] === "function")
    {
        var standardPostBack = window["__doPostBack"];

        window["__doPostBack"] = function(eventTarget, eventArgument) 
        {        
            // Loop through UniqueID hierarchy of the eventTarget looking for a UniqueID that can be 
            // found in the registeredRegions collection. The first one found is that of the 
            // UpdateRegion that needs to have its HTML updated. If an UpdateRegion cannot be found, 
            // then a standard postback will be triggered.
            var updateRegion = null;
            
            var delimiterIndex = -1;
            var eventTargetCopy = eventTarget;
            
            while ((delimiterIndex = eventTargetCopy.lastIndexOf("$")) > -1)
            {
                updateRegion = AjaxControlFramework.UpdateRegion.RegisteredRegions[(eventTargetCopy = eventTargetCopy.substring(0, delimiterIndex)).replace(/\$/g, '_')];
				//updateRegion = AjaxControlFramework.RegisteredControls[(eventTargetCopy = eventTargetCopy.substring(0, delimiterIndex)).replace(/\$/g, '_')];
                
                if (updateRegion != null)
                {
                    break;
                }
            }
            
            
            if (updateRegion != null)
            {
                theForm.__EVENTTARGET.value = eventTarget;
                theForm.__EVENTARGUMENT.value = eventArgument;
                
                updateRegion.Update(function() { 
                        theForm.__EVENTTARGET.value = "";
                        theForm.__EVENTARGUMENT.value = "";
                    }, function(exception) { 
                        theForm.__EVENTTARGET.value = "";
                        theForm.__EVENTARGUMENT.value = "";
                        AjaxControlFramework.DisplayException(exception);
                    }, true
                );
            }
            else
            {
                standardPostBack(eventTarget, eventArgument);
            }
        };
    }
    else
    {
        var inputs = document.getElementsByTagName("input");
        
        for (var index = 0; index < inputs.length; index++)
        {
            if (inputs[index].onclick !== null)
            {
                inputs[index].onclick = function(e) {
                    this.onclick(e);
                    this.focus();
                };
            }
            else
            {
                inputs[index].onclick = function(e) {
                    this.focus();
                };
            }
        }
        
        
        document.forms[0].onsubmit = function(e) {
            var eventTarget = null;
            var updateRegion = null;
            var evt = window.event || e;
            
            if (document.activeElement)
            {
                eventTarget = document.activeElement.getAttribute("name");
            }
            else if (evt.explicitOriginalTarget)
            {
                eventTarget = window.selection.focusNode.name;
            }
            else 
            {
                return;
            }
                
            var delimiterIndex = -1;
            var eventTargetCopy = document.activeElement.name;
            
            while ((delimiterIndex = eventTargetCopy.lastIndexOf("$")) > -1)
            {
                updateRegion = AjaxControlFramework.UpdateRegion.RegisteredRegions[(eventTargetCopy = eventTargetCopy.substring(0, delimiterIndex)).replace(/\$/g, '_')];
				//updateRegion = AjaxControlFramework.RegisteredControls[(eventTargetCopy = eventTargetCopy.substring(0, delimiterIndex)).replace(/\$/g, '_')];
                
                if (updateRegion != null)
                {
                    break;
                }
            }
            
            
            if (updateRegion != null)
            {
                if (!document.forms[0].__EVENTTARGET)
                {
                    var eventTargetInput = document.createElement("input");
                    eventTargetInput.setAttribute("id", "__EVENTTARGET");
                    eventTargetInput.setAttribute("type", "hidden");
                    eventTargetInput.value = document.activeElement.name;
                    document.forms[0].appendChild(eventTargetInput);
                }
                else
                {
                    document.forms[0].__EVENTTARGET.value = document.activeElement.name;
                }
                
                
                if (!document.forms[0].__EVENTARGUMENT)
                {
                    var eventArgumentInput = document.createElement("input");
                    eventArgumentInput.setAttribute("id", "__EVENTARGUMENT");
                    eventArgumentInput.setAttribute("type", "hidden");
                    eventArgumentInput.value = document.activeElement.value;
                    document.forms[0].appendChild(eventArgumentInput);
                }
                else
                {
                    document.forms[0].__EVENTARGUMENT.value = document.activeElement.value;
                }
                
                updateRegion.Update(function(result) { }, function(exception) { 
                    AjaxControlFramework.DisplayException(exception);
                }, true);
                
                return false;
            }
        };
    }
    
})();